package com.umpay.business.consume.impl;

import com.umpay.business.consume.ConsumeScancodeMainBusinessService;
import com.umpay.dict.DataDict;
import com.umpay.dict.retcode.MerAccessEC;
import com.umpay.dict.retcode.MerBusiEC;
import com.umpay.dto.UMFResponse.Message;
import com.umpay.dto.UMFResponse.Message.Meta;
import com.umpay.dto.merAccess.req.consume.ConsumeScancodeMainReq;
import com.umpay.dto.merAccess.res.consume.ConsumeScancodeMainRes;
import com.umpay.dto.merBusi.req.consume.ConsumeScancodeMainBusiReq;
import com.umpay.dto.merBusi.res.consume.ConsumeScancodeMainBusiRes;
import com.umpay.exception.BusinessException;
import com.umpay.feign.client.ConsumeBusiServerFeignClient;
import com.umpay.util.PropertiesUtil;
import com.umpay.util.RpidUtils;
import com.umpay.util.StringUtils;
import com.umpay.util.TimeUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*******************************************************************
 * <br>
 * description : 扫码主扫请求业务接口处理类
 * @author liuhaidong
 * @date 2018年3月23日 
 * @version 1.0
 ******************************************************************/
@Service
public class ConsumeScancodeMainBusinessServiceImpl implements ConsumeScancodeMainBusinessService {

	public static final Logger log = LogManager.getLogger(ConsumeScancodeMainBusinessServiceImpl.class);
	@Autowired
	private ConsumeBusiServerFeignClient consumeBusiServerFeignClient;
	
    public Message<ConsumeScancodeMainRes> consumeScancodeMain(ConsumeScancodeMainReq req){
    	  log.info(String.format("扫码主扫请请求参数为:%s", req));
          String version = StringUtils.trim(req.getVersion()); //版本号
          String notifyUrl = StringUtils.trim(req.getNotify_url()); // 服务器异步通知路径
          String merId = StringUtils.trim(req.getMer_id()); // 商户号
          String merTrace = StringUtils.trim(req.getMer_trace()); //支付交易流水
          String tradeNo = StringUtils.trim(req.getTrade_no()); //联动交易号
          String payType = StringUtils.trim(req.getPay_type()); //支付类型
          
          if(DataDict.PAYTYPE_AL.equals(payType)) { //AL：  支付宝扫码支付
        	  payType = DataDict.PAYTYPE_TRANS_7;
          }else if(DataDict.PAYTYPE_WX.equals(payType)) { //WX：  微信扫码支付 
        	  payType = DataDict.PAYTYPE_TRANS_6;
          } else if (DataDict.PAYTYPE_YL.equals(payType)) {
              payType = DataDict.PAYTYPE_TRANS_M1;
          } else {
              log.info(String.format("请求的参数[payType]=%s不正确", payType));
              throw new BusinessException(MerBusiEC.SCANCODE_PAYTYPE_ERROR, "请求的参数payType不正确");
          }
          String amount = StringUtils.trim(req.getAmount()); // 金额
          String expairTime = StringUtils.trim(req.getExpire_time()); // 金额
          String remark = StringUtils.trim(req.getRemark()); // 备注信息
          String accessType = StringUtils.trim(req.getAccessType());  //接入类型
  		  String accessName = StringUtils.trim(req.getAccessName());  //接入名称
  		  String depBankMerId = StringUtils.trim(req.getDepBankMerId());  //存管行
  		  String goodsInf = StringUtils.trim(req.getGoods_inf());
  		  String goodsId = StringUtils.trim(req.getGoods_id());
          // 组织请求merBusi参数
          ConsumeScancodeMainBusiReq consumeScancodeMainBusiReq = new ConsumeScancodeMainBusiReq();
          consumeScancodeMainBusiReq.setRpid(RpidUtils.getRpid());// 通信流水
          consumeScancodeMainBusiReq.setVersion(version);// 版本号
          consumeScancodeMainBusiReq.setReqDate(TimeUtil.date8());// 请求日期
          consumeScancodeMainBusiReq.setReqTime(TimeUtil.time6());// 请求时间
          consumeScancodeMainBusiReq.setMerId(merId); // 商户号
          consumeScancodeMainBusiReq.setTradeNo(tradeNo); // 联动交易号
          consumeScancodeMainBusiReq.setMerTrace(merTrace); // 支付交易流水
          consumeScancodeMainBusiReq.setPayType(payType); // 支付方式
          consumeScancodeMainBusiReq.setExpireTime(expairTime); // 支付方式
          consumeScancodeMainBusiReq.setAmount(amount); // 金额
          consumeScancodeMainBusiReq.setNotifyUrl(notifyUrl); // 服务器异步通知路径
          consumeScancodeMainBusiReq.setRemark(remark); // 备注信息
          String bProductId = PropertiesUtil.getBusiStrValue("bsp.bproductid", ""); // 业务产品号
          consumeScancodeMainBusiReq.setbProductId(bProductId); // 业务产品号
          String pProductId = "";
          if(DataDict.PAYTYPE_TRANS_7.equals(payType)) { //AL：  支付宝扫码支付
        	  pProductId = PropertiesUtil.getBusiStrValue("bsp.scancodemainAL.pproductid", ""); // 支付产品号
          }else if(DataDict.PAYTYPE_TRANS_6.equals(payType)) { //WX：  微信扫码支付 
        	  pProductId = PropertiesUtil.getBusiStrValue("bsp.scancodemainWX.pproductid", ""); // 支付产品号
          } else if (DataDict.PAYTYPE_TRANS_M1.equals(payType)) {//银联二维码支付
              pProductId = PropertiesUtil.getBusiStrValue("bsp.unionPayQR.push.base.pproductid", ""); // 银联二维码主扫-基础支付产品号
          }
          consumeScancodeMainBusiReq.setpProductId(pProductId); // 原子产品号
          consumeScancodeMainBusiReq.setAccessType(accessType);
          consumeScancodeMainBusiReq.setAccessName(accessName);
          consumeScancodeMainBusiReq.setDepBankMerId(depBankMerId);
          consumeScancodeMainBusiReq.setGoodsInf(goodsInf);
          consumeScancodeMainBusiReq.setGoodsId(goodsId);
          log.info(String.format("组织扫码支付数据完成: %s", consumeScancodeMainBusiReq));
          // 请求交易模块merBusi
          log.info(String.format("扫码支付，调用远程服务接口请求参数为:%s", consumeScancodeMainBusiReq));
          Message<ConsumeScancodeMainBusiRes> merBusiRes = this.consumeBusiServerFeignClient.consumeScancodeMainBusiReq(consumeScancodeMainBusiReq);
          log.info(String.format("扫码支付，调用远程服务接口获取结果为:%s", merBusiRes));

          // 组织接入模块响应结果
          Message<ConsumeScancodeMainRes> resMessage = new Message<ConsumeScancodeMainRes>();
          Meta resMeta = new Meta();
          // 判断业务响应结果，不为空
          if (merBusiRes != null) {
              Meta metaData = merBusiRes.getMeta();
              String retCode = metaData.getRet_code();
              String retMsg = metaData.getRet_msg();
              resMeta.setRet_code(retCode);// 返回码
              resMeta.setRet_msg(retMsg);// 返回描述

              if (MerAccessEC.SUCCESS.equals(retCode)) {// 成功
            	  ConsumeScancodeMainBusiRes resInfo = merBusiRes.getData();
            	  ConsumeScancodeMainRes consumeScancodeMainRes = new ConsumeScancodeMainRes();
                  consumeScancodeMainRes.setMer_id(merId);// 商户号
                  consumeScancodeMainRes.setVersion(DataDict.VERSION);// 版本号
                  consumeScancodeMainRes.setOrder_id(StringUtils.trim(resInfo.getOrderId()));// 商户订单号
                  consumeScancodeMainRes.setMer_date(StringUtils.trim(resInfo.getOrderDate()));// 订单日期
                  consumeScancodeMainRes.setTrade_no(StringUtils.trim(resInfo.getTradeNo()));// 平台返回流水号
                  consumeScancodeMainRes.setMer_trace(StringUtils.trim(resInfo.getMerTrace()));//支付交易流水
                  consumeScancodeMainRes.setAmount(StringUtils.trim(resInfo.getAmount()));// 金额
                  consumeScancodeMainRes.setPayment_link(StringUtils.trim(resInfo.getPaymentLink()));// 缴费二维码地址
                  resMessage.setData(consumeScancodeMainRes);
              }
          } else {// 响应结果为空，交易超时
              resMeta.setRet_code(MerAccessEC.MERBUSI_RESPOND_ERR);
              resMeta.setRet_msg("交易结果不明");
          }

          resMessage.setMeta(resMeta);
          resMessage.setLinks(null);
          return resMessage;
    }
}
